package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.AskAnswerRecordDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 问卷调查记录
 * */

@RestController
@RequestMapping("AskAnswerRecord")
public class AskAnswerRecordController extends BaseController{


    @Autowired
    @Qualifier("AskAnswerRecordServiceImplOne")
    private AskAnswerRecordService askAnswerRecodService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("PracticeSiteServiceImplOne")
    private PracticeSiteService practiceSiteService;

    @Autowired
    @Qualifier("AskAnswerNameServiceImplOne")
    private AskAnswerNameService askAnswerNameService;

    @Autowired
    @Qualifier("AskAnswerRecordSiteServiceImplOne")
    private AskAnswerRecordSiteService askAnswerRecordSiteService;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;




    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String practiceSiteCode = jsonObject.getString("practiceSiteCode");

            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerRecord/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(practiceSiteCode);

            AskAnswerName askAnswerName = askAnswerNameService.getByAskAnswerNameCode(askAnswerNameCode);

            AskAnswerRecord askAnswerRecord = askAnswerRecodService.getByAskAnswerNameCode(askAnswerNameCode);
            if(askAnswerRecord == null){
                askAnswerRecord = new AskAnswerRecord();
                askAnswerRecord.setAskAnswerRecordCode(UUID.randomUUID().toString());
                askAnswerRecord.setPracticeSiteCode(practiceSiteCode);
                askAnswerRecord.setName(askAnswerName.getName());
                askAnswerRecord.setStatus(1);
                askAnswerRecord.setCreateTime(new Date());
                askAnswerRecord.setAskAnswerNameCode(askAnswerNameCode);
                askAnswerRecord.setCommandCenterCode(practiceSite.getCommandCenterCode());
                askAnswerRecord.setGovernmentCode(practiceSite.getGovernmentCode());
                askAnswerRecodService.insert(askAnswerRecord);
            }


            JSONObject jsonObject1 =new JSONObject();
            jsonObject1.put("askAnswerRecord",askAnswerRecord);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "UserAdd", method = RequestMethod.POST)
    public MyResult UserAdd(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");
            String type = jsonObject.getString("type");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerRecord/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            AskAnswerName askAnswerName = askAnswerNameService.getByAskAnswerNameCode(askAnswerNameCode);

            AskAnswerRecord askAnswerRecord = askAnswerRecodService.getByAskAnswerNameCode(askAnswerNameCode);
            if(askAnswerRecord == null){
                askAnswerRecord = new AskAnswerRecord();
                askAnswerRecord.setAskAnswerRecordCode(UUID.randomUUID().toString());
                //askAnswerRecord.setPracticeSiteCode(practiceSiteCode);
                askAnswerRecord.setName(askAnswerName.getName());
                askAnswerRecord.setStatus(1);
                askAnswerRecord.setCreateTime(new Date());
                askAnswerRecord.setAskAnswerNameCode(askAnswerNameCode);
                //askAnswerRecord.setCommandCenterCode(practiceSite.getCommandCenterCode());
                askAnswerRecord.setGovernmentCode(user.getGovernmentCode());
                askAnswerRecord.setType(type);
                askAnswerRecord.setUserCode(userCode);
                askAnswerRecodService.insert(askAnswerRecord);
            }


            JSONObject jsonObject1 =new JSONObject();
            jsonObject1.put("askAnswerRecord",askAnswerRecord);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String askAnswerRecordCode = jsonObject.getString("askAnswerRecordCode");
            int status = jsonObject.getInteger("status");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerRecord/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            AskAnswerRecord askAnswerRecord = askAnswerRecodService.getByAskAnswerRecordCode(askAnswerRecordCode);
            askAnswerRecord.setName(name);
            askAnswerRecord.setStatus(status);
            askAnswerRecodService.update(askAnswerRecord);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //问卷调查次数的列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String practiceSiteCode = jsonObject.getString("practiceSiteCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerRecord/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<AskAnswerRecord> pageInfo = askAnswerRecodService.PageByGovernmentCodePracticeSiteCode(pageIndex,pageSize,
                    staff.getGovernmentCode(),practiceSiteCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //小程序查看问卷调查次数的列表
    @RequestMapping(value = "PageXCX",method = RequestMethod.POST)
    public MyResult PageXCX(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String practiceSiteCode = jsonObject.getString("practiceSiteCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerRecord/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(practiceSiteCode);

            PageInfo<AskAnswerRecord> pageInfo = askAnswerRecodService.PageByGovernmentCodePracticeSiteCode(pageIndex,pageSize,
                    practiceSite.getGovernmentCode(),practiceSiteCode);

            List<AskAnswerRecordDTO> listDTO = new ArrayList<>();
            for(AskAnswerRecord askAnswerRecord:pageInfo.getList()){

                AskAnswerRecordDTO askAnswerRecordDTO = mapper.map(askAnswerRecord,AskAnswerRecordDTO.class);

                List<AskAnswerRecordSite> listAskAnswerRecordSite =
                        askAnswerRecordSiteService.listByAskAnswerRecordCode(askAnswerRecord.getAskAnswerRecordCode());

                askAnswerRecordDTO.setListAskAnswerSite(listAskAnswerRecordSite);

                listDTO.add(askAnswerRecordDTO);
            }

            PageInfo<AskAnswerRecordDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }




}
